using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SpellingQuiz
{
    public partial class UserSelectionForm : Form
    {
        MenuForm menuForm = new MenuForm();

        public UserSelectionForm()
        {
            InitializeComponent();

            foreach (SpellingUser user in Utilities.GetData())
            {
                studentListBox.Items.Add(user.UserName);
            }
        }

        private void addStudentButton_Click(object sender, EventArgs e)
        {
            SpellingUser newUser = new SpellingUser();
            newUser.UserName = newStudentTextBox.Text;

            Utilities.GetData().Add(newUser);
            studentListBox.Items.Add(newUser.UserName);
            Utilities.SaveData();

            newStudentTextBox.Text = "";
        }

        private void removeStudentButton_Click(object sender, EventArgs e)
        {
            int idx = studentListBox.SelectedIndex;

            if (idx == -1) return;
            else
            {
                Utilities.GetData().RemoveAt(idx);
                studentListBox.Items.RemoveAt(idx);
                Utilities.SaveData();
            }
        }

        private void beginButton_Click(object sender, EventArgs e)
        {
            BeginUser();
        }

        private void BeginUser()
        {
            int idx = studentListBox.SelectedIndex;

            if (idx == -1)
            {
                MessageBox.Show("Please select a student first!");
                return;
            }
            else
            {
                SpellingUser user = Utilities.GetData()[idx];
                Utilities.SetCurrentUser(user);

                this.Visible = false;
                menuForm.ShowDialog();
                this.Visible = true;
            }
        }

        private void quitButton_Click(object sender, EventArgs e)
        {
            Utilities.SaveData();
            Application.Exit();
        }

        private void studentListBox_DoubleClick(object sender, EventArgs e)
        {
            BeginUser();
        }
    }
}